import json
from py3db.mysql import MySql
from config.db import DB_CONFIG
from fetch.duplicate_removal import DuplicateRemoval


class GenerateUrls:
    def __init__(self, app_code):
        self.app_code = app_code
        self.duplicate = DuplicateRemoval(app_code)

    def make_url(self, layout_info=None, filed_dict={}):
        db = MySql(DB_CONFIG['host'], DB_CONFIG['user'], DB_CONFIG['password'], self.app_code)
        if layout_info and len(layout_info):
            url_format = layout_info[2]
            param_list = self.get_param_list(layout_info[0])
            url_list = self.get_url_list(param_list, url_format)
            if layout_info[2] == "auto":
                for key, value in filed_dict.items():
                    if not value.get("listing"):
                        url_list = tuple(map(lambda x: x[0], db.select(key, ['next_url'], condition_dict={
                            "next_layout_id": layout_info[0],
                            "picture_content": None
                        })))
                        break
        db.close_connect()
        return url_list if len(url_list) and url_list[0] != "auto" else []

    def get_param_list(self, layout_id):
        db = MySql(DB_CONFIG['host'], DB_CONFIG['user'], DB_CONFIG['password'], "crawler")
        param_list = db.select(
            "crawler_params",
            field_list=["params_name", "params_value", "params_type"],
            condition_dict={"layout_id": layout_id}
        )
        db.close_connect()
        return param_list

    def get_url_list(self, param_list=[], url_format=""):
        url_list = [url_format]
        param_index = 0
        for param in param_list:
            param_type = param[2]
            url_length = len(url_list)
            temp_list = getattr(self, f"get_url_by_{param_type}")(
                param=param[1],
                url_list=url_list,
                param_index=param_index,
                url_length=url_length
            )
            param_index += 1
        # print(url_list)
        print("url_list", url_list)
        return url_list

    def get_url_by_range(self, param, url_list=[], param_index=0, url_length=0):
        begin, end = json.loads(param)
        for i in range(0, url_length):
            url_pop = url_list.pop(0)
            for key in range(begin, end):
                url = url_pop.replace("{%s}" % param_index, f"{key}")
                if not self.duplicate.check_same_url(url):
                    url_list.append(url)
        return url_list

    def get_url_by_list(self, param, url_list=[], param_index=0, url_length=0):
        param_data = json.loads(param)
        url_length = len(url_list)
        for i in range(0, url_length):
            url_pop = url_list.pop(0)
            for key in param_data:
                url = url_pop.replace("{%s}" % param_index, f"{key}")
                if not self.duplicate.check_same_url(url):
                    url_list.append(url)
        return url_list


if __name__ == '__main__':
    a = "{0}test{1}".format("111", "222")
    print(a)
